﻿/*
 * lib-BNet (Bass.Net)
 * 
 * The point of this class is to simply simplify the process of interactiong
 * between the BNet library, and the programmer. It removes the accessablility
 * to a large majority of the features (such as tags, wave info, etc), and
 * prioritizes the essentials (Play, Pause, Stop, Seek, Initilaize)
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Tags;

namespace Genesis
{
    class libBNet
    {
        private static int stream;

        public static string Status = "";

        public static void Initialize()
        {
            BassNet.Registration("genesis@programming.com", "2X15232823253722");
            if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero))
            {
            }
        }
        public static void Volume(int volume)
        {
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_GVOL_STREAM, volume * 100);

        }
        public static void Seek(double sec)
        {
            if (stream != 0)
            {
                Bass.BASS_ChannelSetPosition(stream, sec);
            }
        }

        public static void Play(string path, bool restart)
        {
            Bass.BASS_StreamFree(stream);
            stream = Bass.BASS_StreamCreateFile(path, 0L, 0L, BASSFlag.BASS_DEFAULT);
            Bass.BASS_ChannelPlay(stream, restart);
            Status = "Play";
        }
        public static void Pause()
        {
            // Ensure's that the user cannot pause nothing.
            if (stream != 0)
            {
                Bass.BASS_ChannelPause(stream);
                Status = "Paused";
            }
        }
        public static void Resume()
        {
            if (stream != 0)
            {
                Bass.BASS_ChannelPlay(stream, false);
            }
        }
        public static void Stop()
        {
            Bass.BASS_Stop();
            Bass.BASS_StreamFree(stream);
            Status = "Stop";
        }

        public static double GetPosition
        {
            get 
            {
                if (stream != 0)
                    return Bass.BASS_ChannelBytes2Seconds(stream, Bass.BASS_ChannelGetPosition(stream));
                else
                    return 0;
            }
        }
        public static double GetLength
        {
            get
            {
                if (stream != 0)
                    return Bass.BASS_ChannelBytes2Seconds(stream, Bass.BASS_ChannelGetLength(stream));
                else
                    return 0;
            }
        }

        public static Song GetInfo(string path)
        {
            Song newSong = new Song();
            int newStream = Bass.BASS_StreamCreateFile(path, 0L, 0L, BASSFlag.BASS_DEFAULT);
            TAG_INFO tagInfo = new TAG_INFO(path);
            if (BassTags.BASS_TAG_GetFromFile(newStream, tagInfo))
            {
                newSong.Title = tagInfo.title;
                newSong.Artist = tagInfo.artist;
                newSong.Album = tagInfo.album;
                newSong.Genre = tagInfo.genre;
                newSong.PlayCount = 0;
                newSong.Time = Convert_Time(Convert.ToInt32(Bass.BASS_ChannelBytes2Seconds(newStream, Bass.BASS_ChannelGetLength(newStream))));
                newSong.Location = path;
                newSong.Date = DateTime.Now;
            }
            string[] p = path.Split('\\');
            string filename = p[p.Length - 1];

            if (newSong.Title == "")
                newSong.Title = filename;

            return newSong;
        }
        private static string Convert_Time(int sec)
        {
            string min;
            string second;

            second = Convert.ToString(sec % 60);
            min = Convert.ToString(RoundDown(sec / 60));

            if (second.Length != 2)
                second = "0" + second;

            return min + ":" + second;
        }
        private static double RoundDown(double valueToRound)
        {
            double floorValue = Math.Floor(valueToRound);
            if ((valueToRound - floorValue) > .5)
            {
                return (floorValue + 1);
            }
            else
            {
                return (floorValue);
            }
        }
    }
}
